<?php


namespace api\blog\controller;


use api\blog\model\GoodsModel;
use api\blog\model\OrderModel;
use cmf\controller\RestBaseController;
use think\facade\Db;
use think\Request;

class OrderController extends RestBaseController
{
    //获取商品信息
    public function getGoodsList()
    {
        //获取搜索字段
        $name = input('name');
        $page = input('page/d', 1);
        $listRow = input('list_row/d', 10);
        //查询商品
        $goods = GoodsModel::with('classify')
            ->order(['id' => 'DESC'])
            ->field('id,name,source,photos_url,status,gid')
            ->where('name', 'like', '%' . $name . '%')
            ->page($page, $listRow)
            ->select();
        $this->success('商品列表', $goods);
    }

    //商品详情
    public function info()
    {
//        $arr=[
//            '颜色' => ['红色', '黑色','白色'],
//            '尺码' => ['M', 'L','XL'],
//        ];
//        halt(json_encode($arr, JSON_UNESCAPED_UNICODE));
        $id = input('id/d');
        if (!$id) {
            $this->error('商品不存在');
        }
        $goods = GoodsModel::field('goods_list')->find($id);
        $goodsAttr = json_decode($goods['goods_list'], true);
        foreach ($goodsAttr as $key => $val) {
            $goodsAttr[$key]['attr_value'] = explode(',', $val['attr_value']);
        }
        $goods['goods_list'] = $goodsAttr;
        return success('商品详情', $goods);
    }

    //商品订单支付信息
    public function order(Request $request)
    {
        $data = $request->post();
//        $info=[
//            ['颜色'=>'黑色','尺码'=>'L','价格'=>'122'],
//            ['颜色'=>'红色','尺码'=>'M','价格'=>'122']
//        ];
//        halt(json_encode($info,JSON_UNESCAPED_UNICODE));
        //查询商品
        $goods = GoodsModel::where('id', $data['goods_id'])->find();

        $list = $this->getGoodsInfo($goods, $data['goods_info']);
        if (!$goods) {
            return error('该商品不存在');
        }
        if ($goods['status'] != 1) {
            return error('该商品已下架无法购买');
        }
        $address = Db::name('delivery')->where('id', $data['address_id'])->find();
        if (!$address) {
            return error('地址不存在');
        }
        $insert_data = [
            'user_id' => $request->userId,
            'goods_name' => $goods['name'],
            'goods_info' => json_encode($data['goods_info']),
            'price' => $list['价格'],
            'num' => $data['num'],
            'total_price' => round($goods['freight'] * $data['num'], 2),
            'trade_no' => date('YmdHis') . mt_rand(1111, 9999),
            'user_name' => $address['consignee'],
            'user_tel' => $address['mobile'],
            'user_address' => $address['area'] . $address['address'],
            'pay_type' => $data['pay_type'],
            'status' => 0,
            'remarks' => $data['remarks'],
            'create_time' => date('Y-m-d H:i:s'),
        ];
        $model = new OrderModel();
        if ($model->save($insert_data)) {
            return json(['code' => '1', 'message' => '订单创建成功', 'data' => [
                'payInfo' => [
                    'order_id' => $model->id
                ]
            ]]);
        } else {
            return json(['code' => '0', 'message' => '订单创建失败']);
        }
    }

    //查询订单详情
    protected function getGoodsInfo($goods, $spe)
    {
        $goodsSpe = json_decode($goods['info'], true);
        // halt($spe);
        foreach ($goodsSpe as $sku) {
            $hasSku = true;
            //购买的规格
            foreach ($spe as $key => $val) {
                if ($sku[$key] != $val) {
                    $hasSku = false;
                }
            }
            if ($hasSku) {
                return $sku;
            } else {
                continue;
            }
        }
    }
}